import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter("http://127.0.0.1:5500/src/user.html")//在这里填入拦截的请求路径，比如/user/*
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse=(HttpServletResponse) servletResponse;

//        设置字符集 utf-8
        httpServletRequest.setCharacterEncoding("UTF-8");
        httpServletResponse.setCharacterEncoding("UTF-8");

//        处理跨区域访问
//        填入Access-Control-Orign  ，以及访问的区域,      “*”代表所有的区域都可以访问
        httpServletResponse.setHeader("Access-Control-Orign","http://127.0.0.1:5500/src/user.html");
//        代表可以获取的方式(或者称为允许访问的方式)
        httpServletResponse.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS");
//          代表在客户端请求响应的时间，单位为秒
        httpServletResponse.setHeader("Access-Control-Max-Age","3628800");
//          响应头处理      x-requested-with,Authorization 授权
        httpServletResponse.setHeader("Access-Control-Allow-Headers","x-requested-with,Authorization");

    }

    @Override
    public void destroy() {

    }
}
